public class MySingList implements ILIst {
    //节点的内部类
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
            next = null;
        }
    }

    //头指针
    public ListNode head;

    public void creatList() {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(5);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        head = node1;
    }

    @Override
    public void addFirst(int data) {
        ListNode newNode = new ListNode(data);
        if (head == null) {
            this.head = newNode;
        } else {
            newNode.next = this.head;
            this.head = newNode;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode newNode = new ListNode(data);
        if (head == null) {
            this.head = newNode;
        } else {
            ListNode cur = this.head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = newNode;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        if(index<0||index>size()){

            return;
        }
        ListNode newNode = new ListNode(data);
        if(index==0) {
            newNode.next = this.head;
            this.head = newNode;
        }else {
            int count = 0;
            ListNode cur = this.head;
            while (count < index - 1) {
                cur = cur.next;
                count++;
            }
            newNode.next = cur.next;
            cur.next = newNode;
        }
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if (head.val == key) {
            head = head.next;
            return;
        }
        ListNode cur = head;
        ListNode prev = head;
        while (cur != null && cur.val != key) {
            prev = cur;
            cur = cur.next;
        }
        prev.next = cur.next;
        cur = null;
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur = this.head;

    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }

    @Override
    public void clear() {

    }

    @Override
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + "->");
            cur = cur.next;
        }
        System.out.println("null");
    }
}
